/*
 * @Author: YWY
 * @Date: 2022-03-04 15:31:51
 * @LastEditors: YWY
 * @LastEditTime: 2022-03-06 18:57:32
 */
import { defineStore } from "pinia";
import { followJug, getUserArticle, getUserFollows, getFans, getUserData, followUser, disFollowUser, getTotalLikes } from "../api/services/user.service";
import { Toast } from "vant";
export const otherUserStore = defineStore('otherUser', {
    state: () => {
        return {
            name: '路人甲',
            sex: '男',
            city: '杭州',
            avatar: "http://localhost:3000/src/assets/image/userImg.png",
            article: [],
            favorite: [],
            follows: [],
            followed: false,
            fans: [],
            likes: 0
        }
    },
    actions: {
        //获取用户发表文章
        async loadUserAticle(id) {
            let res = await getUserArticle(id)
            return res.result.article_list
        },

        //获取用户关注列表
        async loadUserFollows(id) {
            let res = await getUserFollows(id);
            return res.res;
        },

        //获取用户粉丝列表
        async loadUserFans(id) {
            let res = await getFans(id);
            return res.res
        },

        //获取用户资料
        async loadUserData(id) {
            let res = await getUserData(id);
            let data = res.result.user;
            this.$patch(
                {
                    avatar: data.img,
                    name: data.name,
                    city: data.city,
                    sex: data.sex,
                }
            )
        },

        //判断用户是否关注
        async getfollowJug(id) {
            let res = await followJug(id);
            return res.res
        },

        //关注用户
        addFollow(id) {
            followUser(id).then((res) => {
                Toast("关注成功");
                this.followed = true
            });
        },

        //取消关注
        disFollow(id) {
            disFollowUser(id).then((res) => {
                Toast("取消关注成功");
                this.followed = false
            });
        },

        //获取总点赞数
        async loadTotalLikes(id) {
            let res = await getTotalLikes(id);
            return res.result.cnt ? res.result.cnt : 0
        },

        //页面初始化
        async init(id) {
            console.log(id)
            this.loadUserData(id);
            this.$patch(
                {
                    article: await this.loadUserAticle(id),
                    follows: await this.loadUserFollows(id),
                    followed: await this.getfollowJug(id),
                    fans: await this.loadUserFans(id),
                    likes: await this.loadTotalLikes(id)
                }
            )
        }
    }
})